import store from '@/store/index.js'
import {
	getTokenSync,setToken
} from './auth.js'


export default {
	permission(page){
		
		
		//获取token
		const hasToken = getTokenSync()

		//白名单
		const whiteList = ['/pages/views/Login/Login','/pages/views/engineer/Personal/Personal','/pages/views/engineer/Massage/Massage','/pages/views/engineer/EquipmentList/EquipmentList']

		/**
		 * @param {Object} hasToken
		 * 判断是否登录
		 */
		console.log(hasToken)
		
		if (!hasToken.length == 0) {
				
			/**
			 *有token 如果当前页面为登录页直接从登录页踢回个人中心
			 */
			console.log(page)
			if (page === '/pages/views/Login/Login') {
				uni.redirectTo({
					url: ('/pages/views/engineer/Personal/Personal')
				})
			} else {
				const hasUserId = store.getters.id
				const haseUserName = store.getters.name
				/**
				 * 是否有用户信息没有获取用户个人信息
				 */
				console.log("当前用户id为"+hasUserId)
				console.log(hasUserId)
				console.log(hasUserId)
				if (haseUserName.length == 0) {
					store.dispatch("user/getInfo",hasUserId).then((res)=>{
						uni.hideLoading()
					})
					
				}
			}
		} else {
			console.log(page)
			/* has no token*/
			console.log(whiteList.indexOf(page))
			if (whiteList.indexOf(page) == -1) {
				// 不在白名单则会被重定向到登录页面
				uni.reLaunch({
					url: ('/pages/views/Login/Login')
				})
				
			}
		}

	}
}
